﻿#define _CRT_SECURE_NO_WARNINGS 1

/*
单值二叉树
二叉树的遍历有前序中序后序
*/

//每日一题_08
//965. 单值二叉树
//https://leetcode.cn/problems/univalued-binary-tree/description/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
public:
    bool PrevOrder(TreeNode* root, int value) {
        if (root == NULL)
            return true;
        if (root->val != value)
            return false;
        else
            return (PrevOrder(root->left, value) && PrevOrder(root->right, value));
    }
    bool isUnivalTree(TreeNode* root) {
        // 每个树的节点值都是相同的就是单值二叉树
        // 所以只要遍历二叉树看值相不相同就行
        // 前序遍历
        return PrevOrder(root, root->val);
    }
};